package br.ucb.conversor;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

import br.ucb.JPA.LigaDAO;
import br.ucb.negocio.Liga;

@FacesConverter( value="LigaConverter" )
public class LigaConverter implements Converter {

	public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
		if (value == null)
			return null;
		try {
			return new LigaDAO().consultar(Long.parseLong(value));
		} catch (Exception e) {
			e.printStackTrace();
			return new Object();
		}
	}

	public String getAsString(FacesContext context, UIComponent component, Object object) throws ConverterException {
		if (object == null){
			return null;
		}
		Liga liga = (Liga) object;
		return Long.toString(liga.getId());
	}

}